package com.dingli.servlet;

import com.dingli.DAO.RegisterAndLoginDAO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;

@WebServlet(name = "RegisterServlet", urlPatterns = "/register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/views/register.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        String confirm_password = request.getParameter("confirm-password");

        if (!password.equals(confirm_password)) {
            response.sendRedirect("/error");
            return;
        }
        try {
            RegisterAndLoginDAO logoutDAO = new RegisterAndLoginDAO();
            int flag = logoutDAO.logout(account, password);
            if (flag > 0) {
                // 注册成功
                response.sendRedirect("/login");
            } else {
                // 注册失败
                response.sendRedirect("/error");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            response.sendRedirect("/error");
        }
    }
}


